package Game;

import java.util.ArrayList;
import java.util.Arrays;

import Helper.ArrayHelper;

public class Damage implements IEffectTarget {
	Unit source;
	Unit target;
	
	int baseDamage = 0;
	ArrayList<Double> attackModifiers = new ArrayList<>();
	ArrayList<Double> damageModifiers = new ArrayList<>();
	int bonusDamage = 0;
	
	int element = Element.ELEMENT_NORMAL;
	boolean magical = false;
	boolean healing = false;
	boolean fromSkill = false;
	
	boolean toHP = true;
	boolean toMP = false;
	boolean toAP = false;
	
	boolean compute = true;
	int totalDamage = 0;
	
	/**
	 * Creates a Damage instance with the specified base damage.
	 * @param _baseDamage Base damage
	 */
	public Damage(int _baseDamage, Unit _source, Unit _target) {
		baseDamage = _baseDamage;
		source = _source;
		target = _target;
	}
	
	/**
	 * Computes and sets total damage based on this Damage's attributes.
	 * @return Total damage serangan
	 */
	public int compute() {
		if (!compute)
			return totalDamage;
		
		double totalAttackMod = ArrayHelper.multiplyArray(attackModifiers);
		double totalDamageMod = ArrayHelper.multiplyArray(damageModifiers);
		
		addElementModifier();
		
		totalDamage = (int)((baseDamage*totalAttackMod)*totalDamageMod + bonusDamage);
		
		if (healing)
			totalDamage *= -1;
		
		return totalDamage;
	}
	
	/**
	 * Incurs damage to target and activates any damage effects.
	 */
	public void incur() {
		compute();
		
		if (toHP)
			target.incHP(-totalDamage);
		if (toMP)
			target.incMP(-totalDamage);
		if (toAP)
			target.incAP(-totalDamage);
	}
	
	public void addElementModifier() {
		int elementResistance = target.elementalResistance[element];
		
		if (elementResistance > 100) {
			damageModifiers.add(((double)elementResistance - 100)/100);
			healing = true;
		} else if (elementResistance == 100) {
			compute = false;
			totalDamage = 0;
		} else {
			damageModifiers.add(1 - (double)elementResistance/100);
		}
	}
	
	public boolean isElemental() {
		return (element != Element.ELEMENT_NORMAL) && (element != Element.ELEMENT_HOLY) && 
			(element != Element.ELEMENT_DARKNESS); 
	}
	
	public static void main(String[] args) {
	}
}
